import * as vscode from 'vscode';
import * as path from 'path';
import { init } from './vsCommand/common';

import {
	LanguageClient,
	LanguageClientOptions,
	ServerOptions,
	TransportKind
} from 'vscode-languageclient/node';

let client: LanguageClient;


export function activate(context: vscode.ExtensionContext) {

	init(context);


	//#region  创建LSP服务
	const serverModule = context.asAbsolutePath(
		path.join( 'out', 'server.js')
	);
	// 开发调试链接
	const debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };
	// 创建server配置
	const serverOptions: ServerOptions = {
		run: { module: serverModule, transport: TransportKind.ipc },
		debug: {
			module: serverModule,
			transport: TransportKind.ipc,
			options: debugOptions
		}
	};
	// 创建client配置
	const clientOptions: LanguageClientOptions = {
		documentSelector: ['html', 'vue', 'css', 'less', 'scss'],
		// documentSelector: [
		// 	{ scheme: 'file', language: 'plaintext' },
		// 	{ scheme: 'file', language: 'vue' },
		// ],
		synchronize: {
			fileEvents: vscode.workspace.createFileSystemWatcher('**/.clientrc')
		}
	};
	// 创建client对象
	client = new LanguageClient(
		'cssTips',
		'Css tips',
		serverOptions,
		clientOptions
	);
	client.start();
	//#endregion
}


export function deactivate(): Thenable<void> | undefined {
	if (!client) {
		return undefined;
	}
	return client.stop();
}
